from django.shortcuts import render,redirect
from django.http import HttpResponse
from cmdb.models import Hosts,ItemSofts
from cmdb.models import softs as softs_models
from account.models import MyUser
from django.contrib.auth.decorators import login_required
import json
from salt_api.execute import ex_hosts
from cmdb.forms import softsForm,items_softForm,ItemsForm

@login_required
def index(request):
    counts = {}
    counts['host_count'] = Hosts.objects.all().count()
    counts['user_count'] = MyUser.objects.all().count()
    print(counts)
    return render(request,'dashboard.html',counts)
@login_required
def hosts(request):
    objects = Hosts.objects.values('id','hostcode','ip_wan','ip_lan','configureations','create_date','os','release')
    return render(request,'host.html',{'hosts':objects})

@login_required
def host_init(request):
    # 检查cmdbhost表是否存在数据,存在则为初始化,
    # 第一次初始化需要记录初始化的时间和主机信息
    if request.method == 'POST':
        msg = {}
        if request.is_ajax():
            if request.POST.get('name') == 'host_init':
                hostObj = Hosts.objects.all().count()
                if hostObj > 0:
                    # 已经初始化
                    msg['check'] = 0
                else:
                    # 未初始化
                    ex_host = ex_hosts() # 执行主机初始化
                    ex_host.run('init')
                    msg['check'] = 1

                return HttpResponse(json.dumps(msg))
    return render(request,'host-init.html')


@login_required
def host_refurbish(request):
    if request.method == 'POST':
        msg = {}
        if request.is_ajax():
            if request.POST.get('name') == 'host_refurbish':
                    ex_host = ex_hosts()  # 执行主机初始化
                    ex_host.run('refurbish')
                    return HttpResponse(json.dumps('host refurbish successful!'))
    return render(request,'host_refurbish.html')

@login_required
def users(request):
    return render(request,'user.html')

@login_required
def items(request):
    return render(request,'items.html')

@login_required
def item_add(request):
    form = ItemsForm()
    if request.method == 'POST':
        form = ItemsForm(request.POST)
        if form.is_valid():
            form.save()
            return  redirect('items/')
    return render(request,'items_add.html',{'itemForm':form})



@login_required
def softs(request,args=None):
    softs_obje = softs_models.objects.all()
    return render(request,'soft.html',{'softs':softs_obje})
@login_required
def soft_add(request):
    form = softsForm()
    if request.method == 'POST':
        print(0)
        form = softsForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('softs/')
    return render(request, 'soft_add.html', {'softForm': form})

@login_required
def soft_edit(request,id):
    softs_obje = softs_models.objects.get(pk=id)
    form = softsForm(instance=softs_obje)
    if request.method == 'POST':
        form = softsForm(request.POST,instance=softs_obje)
        if form.is_valid():
            form.save()
            return  redirect('softs/')
    return render(request, 'soft_edit.html', {'softForm': form})


@login_required
def soft_delete(request,id):
    softs_obje = softs_models.objects.get(pk=id).delete()
    form = softs_models.objects.all()
    return render(request, 'soft.html', {'softForm': form})



@login_required
def items_soft(request):
    itemsoft_list = ItemSofts.objects.all()
    return render(request, 'items_soft.html', {'item_softs': itemsoft_list})


@login_required
def items_soft_add(request):
    form = items_softForm()
    if request.method == 'POST':
        form = items_softForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('items_soft/')
    return render(request,'items_soft_add.html',{'ItemsoftForm':form})

@login_required
def items_soft_edit(request,id):
    itemsoft_obj = ItemSofts.objects.get(pk=id)
    form = items_softForm(instance=itemsoft_obj)
    if request.method == 'POST':
        form = items_softForm(request.POST,instance=itemsoft_obj)
        if form.is_valid():
            form.save()
            return  redirect('items_soft/')

    return render(request,'item_soft_edit.html',{"ItemsoftForm":form})

@login_required
def items_soft_delete(request,id):
    ItemSofts.objects.get(pk=id).delete()
    return redirect('items_soft/')

@login_required
def tasks(request):
    return render(request,'tasks.html')

@login_required
def codepush(request):
    return render(request,'codepush.html')

@login_required
def codepush_add(request):
    return render(request,'codepush_add.html')
